package flinty.autocomplete.combobox.test;

import java.awt.AWTEvent;
import java.awt.Container;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

abstract class BaseTestApplet extends JApplet {

	private static final long serialVersionUID = 6340274074879202764L;

	private final JComboBox comboBox = new JComboBox();

	private final JTextField textField = new JTextField();

	@Override
	public void init() {
		textField.setText("100-200,1100-1200");
		JLabel modelLabel = new JLabel("Model:");
		JButton decorateButton = new JButton("Decorate");
		decorateButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String text = textField.getText();
				try {
					LinkedList<Integer> lst = new LinkedList<Integer>();
					for (String str : text.split(",")) {
						int idx = str.indexOf("-");
						if (idx != -1) {
							for (int n = Integer.parseInt(str.substring(0, idx)); n <= Integer
									.parseInt(str.substring(idx + 1)); n++) {
								lst.add(Integer.valueOf(n));
							}
							continue;
						}
					}
					decorateComboBox(comboBox, lst);
				} catch (Exception exc) {
					JOptionPane.showMessageDialog(BaseTestApplet.this,
							"error: " + exc.getMessage());
				}
			}
		});
		Container pane = getContentPane();
		GroupLayout layout = new GroupLayout(pane);
		layout.setAutoCreateContainerGaps(true);
		layout.setAutoCreateGaps(true);
		pane.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)//
				.addGroup(
						layout.createSequentialGroup()
								//
								.addGroup(
										layout.createParallelGroup(
												Alignment.TRAILING)//
												.addComponent(modelLabel)//
												.addComponent(decorateButton)//
								)//
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)//
												.addComponent(textField, 200,
														200, 200)//
												.addComponent(comboBox, 200,
														200, 200)//
								)//
				));
		layout.setVerticalGroup(layout.createSequentialGroup()//
				.addGroup(layout.createParallelGroup(Alignment.BASELINE)//
						.addComponent(modelLabel)//
						.addComponent(textField)//
				).addGroup(layout.createParallelGroup(Alignment.BASELINE)//
						.addComponent(decorateButton)//
						.addComponent(comboBox)//
				));
		layout.linkSize(SwingConstants.VERTICAL, comboBox, textField);
		layout.linkSize(SwingConstants.HORIZONTAL, comboBox, textField);
	};

	@Override
	public void processEvent(AWTEvent e) {
		if (e.getID() == Event.WINDOW_DESTROY) {
			System.exit(0);
		}
	}

	protected abstract void decorateComboBox(JComboBox combo,
			LinkedList<Integer> lst);

}
